<template>
	<view class="integral_container">
		<view class=" integral_top auto">
			<view class="display space-between align-items">
				<view class="display align-items integral_top_name">
					<view class="line"></view>
					转出复购积分
				</view>
				<view class="mx" @click="goJf">明细</view>
			</view>
			
			<view class="code display space-between align-items">
				<input type="text" value="" v-model="ucode" placeholder="请输入对方邀请码" />
				<view @click="seePerplet">查询</view>
			</view>
		</view>
		<!-- 人 -->
		<view class="person">
			<view class="person_container auto">
				<view class="person_contant" v-if="seeUser">
					<view class="person_contant_top display align-items">
						<image :src="imgUrl+seeUser.avatar" mode=""></image>
						<view class="person_top_name">
							<view class="name">{{seeUser.nickname}}</view>
							<view class="phone" v-if="seeUser.mobile!=''">{{seeUser.mobile}}</view>
						</view>
					</view>
					<view class="money display  border_bottom">
						<input type="number" v-model="abs" value="" placeholder="请输入转出积分" />
					</view>
				</view>
				<view class="used">可用复购积分{{UserAccount.score}}</view>
			</view>
		</view>
		<view class="btn display align-items justify-content" @click="submitBtn">确定</view>
		<is-login v-on:childClick='listenToClose' v-on:childClickAgain='listenToCloseAgain' v-if="isLogin==0"></is-login>
	</view>
</template>

<script>
	import isLogin from '../../../components/isLogin.vue'
	export default {
		data() {
			return {
				isLogin: 1,
				UserAccount: {},
				seeUser: null,
				ucode: '',
				abs: '',
				imgUrl: null,
				tid: ''
			}
		},
		onLoad() {
			this.imgUrl = this.$imgUrl
			if (!uni.getStorageSync('openid')) {
				this.isLogin = 0
			} else {
				this.getMoneyInfo()
			}
		},
		onPullDownRefresh() {
			setTimeout(() => {
				this.getMoneyInfo(() => {
					uni.stopPullDownRefresh()
				})
			}, 1000)
		},
		components: {
			isLogin,
		},
		methods: {
			getMoneyInfo(callback) {
				let that = this
				var data = {
					openid: uni.getStorageSync('openid')
				}
				this.$myRequest.post(this.$api.mine.getUserAccount, data).then(res => {
					if (res.code == '200') {
						that.UserAccount = res.data
						callback && callback()
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				})
			},
			// 点击查询
			seePerplet() {
				let that = this
				if (that.ucode == '') {
					return uni.showToast({
						title: '请输入对方的邀请码',
						icon: "none"
					})
				}
				var data = {
					openid: uni.getStorageSync('openid'),
					ucode: that.ucode
				}
				this.$myRequest.post(this.$api.mine.getTransferred, data).then(res => {
					if (res.code == '200') {
						if (res.data.data == null) {
							return uni.showToast({
								title: '请输入正确的邀请码',
								icon: "none"
							})
						}
						that.seeUser = res.data.data
						that.tid = res.data.data.id
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				})
			},
			// 转账
			submitBtn() {
				let that = this
				var tid = that.tid
				var abs = that.abs
				var money = that.UserAccount.score
				var type = 'score'
				abs = Number(abs)
				money = Number(money)

				if (that.tid == '') {
					return uni.showToast({
						title: '请先查询转账人的基本信息',
						icon: "none"
					})
				}
				if (that.abs == '') {
					return uni.showToast({
						title: '请输入要转出的复购积分',
						icon: "none"
					})
				}
				var data = {
					openid: uni.getStorageSync('openid'),
					tid: tid,
					abs: abs,
					type: type
				}
				if (abs - money > 0) {
					return uni.showToast({
						title: '转出积分不能大于所属积分',
						icon: "none"
					})
				}
				this.$myRequest.post(this.$api.mine.transfer, data).then(res => {
					if (res.code == '200') {
						uni.showToast({
							title: '转账成功',
							icon: "success",
							success() {
								setTimeout(function() {
									uni.navigateBack()
								}, 1500)
							}
						})
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				})
			},
			goJf(){
				let that = this
				if (!uni.getStorageSync('openid')) {
					uni.showToast({
						title: '请先登录',
						icon: "none"
					})
				} else {
					var type = 'score'
					uni.navigateTo({
						url: "../accountsDetails/accountsDetails?type="+type
					})
				}
			},
			listenToClose(msg) {
				this.isLogin = msg
			},
			listenToCloseAgain() {
				this.getMoneyInfo()
			},
		}
	}
</script>

<style scoped lang="scss">
	.integral_container {
		.integral_top {
			width: 690rpx;
			padding-top: 12rpx;
	
			.integral_top_name {
				height: 50rpx;
				font-size: 30rpx;
				.line {
					width: 5rpx;
					height: 22rpx;
					background: #4b968d;
					border-radius: 3px;
					margin-top: 8rpx;
					margin-right: 11rpx;
				}
			}
			.mx{
				font-size: 26rpx;
				color:#4b968d ;
			}
	
			.code {
				margin-top: 40rpx;
	
				input {
					width: 580rpx;
					margin-left: 15rpx;
					font-size: 32rpx;
					color: #999999;
				}
	
				view {
					font-size: 26rpx;
					color: #4b968d;
				}
			}
		}
	
		.person {
			margin-top: 30rpx;
			border-top: 15rpx solid #E5E5E5;
	
			.person_container {
				width: 690rpx;
	
				.person_contant {
					margin-top: 41rpx;
	
					.person_contant_top {
						image {
							width: 80rpx;
							height: 80rpx;
						}
	
						.person_top_name {
							margin-left: 28rpx;
	
							.name {
								font-size: 30rpx;
								color: #333333;
							}
	
							.phone {
								color: #999999;
								font-size: 24rpx;
							}
						}
					}
	
					.money {
						font-size: 36rpx;
						margin-top: 35rpx;
						padding-bottom: 25rpx;
	
						input {
							width: 600rpx;
							font-size: 30rpx;
							color: #999999;
							margin-left: 25rpx;
						}
					}
	
				}
	
				.used {
					color: #294D7C;
					font-size: 26rpx;
					margin-top: 20rpx;
					margin-left: 15rpx;
				}
			}
		}
	}
</style>
